'use strict';
const fs = require('fs');
const path = require('path');
const AlipaySdk = require('alipay');
// 普通公钥模式
let alipaySdk = new AlipaySdk({});
alipaySdk=new alipaySdk.default({
  appId: 'xxx',
  privateKey: fs.readFileSync(path.resolve(path.join(__dirname,'./private-key.pem')), 'ascii'),
})
exports.main = async (event, context) => {
	try{
    const result = await alipaySdk.exec('alipay.system.oauth.token', {
    	grantType: 'authorization_code',
    	code: event.code || '',
    });
    console.log(result)
    return {
      code:200,
      message:'操作成功',
      data:result.userId
    }
  }catch(err){
    console.error(err)
    return {
      code:500,
      message:'获取授权用户ID失败',
    }
  }
};
